import requests
from lxml import etree
import csv
import pymysql
def get_html(url):
    try:
        headers = {'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'cookie':'PSTM=1748517189; BAIDUID=96971E02A0A9826917F2EFB83D8AAF90:FG=1; H_PS_PSSID=62325_62969_63147_63194_63210_63241_63268_63326_63357_63369_63384_63379_63186_63392_63391_63401_63440; BAIDUID_BFESS=96971E02A0A9826917F2EFB83D8AAF90:FG=1; BA_HECTOR=ag008ga081ag80ah8k21200kal0k861k3gga924; BIDUPSID=912FDF5BD1489F1FF26F722190A370BC; ZFY=7sXEBICKyCqKbfVj54u9q3JaJXZTP:AXG4Rgn88CS6uA:C; delPer=0; PSINO=2; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598'}
        r = requests.get(url=url,headers=headers)
        r.encoding = r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as err:
        print(err)
def parser(html):
    doc = etree.HTML(html)
    out_list = []
    for i in doc.xpath('//*[@id="content"]/div//ul/li'):
        title = i.xpath('div/h2/a/text()')[0].strip()
        score = i.xpath('div//span/text()')[0].strip()
        info = i.xpath('div/p[2]/text()')[0].strip().split('/')
        describe = i.xpath('div/p[3]/text()')[0].strip()
        i_list=[title,score,info[0],info[1],info[2],describe]
        out_list.append(i_list)
    print(out_list)
    return out_list
def save_mysql(sql,val,**dbinfo):
    try:
        connect = pymysql.connect(**dbinfo)
        cursor = connect.cursor()
        cursor.executemany(sql,val)
        connect.commit()
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        connect.close()
if __name__ == '__main__':
    url = 'http://www.bspider.top/doubanbook/'
    html = get_html(url)
    out_list = parser(html)
    data = {'host':'127.0.0.1',
            'user':'root',
            'password':'root',
            'db':'voot',
            'charset':'utf8',
            'cursorclass':pymysql.cursors.DictCursor}
    sql = 'insert into coop(bookname,score,autor,press,pubdate,describ)'\
    'values(%s,%s,%s,%s,%s,%s)'
    save_mysql(sql,out_list,**data)